﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Utilities;
using BLL;

namespace WorkflowDesign.Controllers
{
    public class WorkflowPreviewController : Controller
    {
        public ViewResult Index(int? workflowid)
        {
            if (workflowid != null)
            {
                var wf = CH.GetDataById<Workflow>(workflowid);
                ViewBag.workflowid = workflowid;
                ViewBag.workflowname = wf.WorkflowName;
                var list = CH.GetAllData<WorkflowInstance>(wi => wi.WorkflowID == workflowid);
                return View(list);
            }
            else
            {
                return View(CH.GetAllData<WorkflowInstance>());
            }
        }

        [HttpPost]
        public ActionResult Start(int workflowid, FormCollection formCollection)
        {
            if (FlowRun.Start(workflowid, formCollection.AllKeys, formCollection.ToValueProvider()))
                return RedirectToAction("index");
            return View();
        }

        public ActionResult Start(int workflowid)
        {
            var wi = FlowRun.GenerateWorkflowInstance(workflowid);
            return View(wi);
        }

        public ActionResult Approve(int id)
        {
            var wi = CH.GetAllData<WorkflowInstance>(i => i.ID == id, "FieldInstances", "ActivityInstances").FirstOrDefault();
            return View(wi);
        }

        [HttpPost]
        public ActionResult Approve(int id, FormCollection formCollection)
        {
           
          if(FlowRun.Approve(id,formCollection.AllKeys,formCollection.ToValueProvider()))
             return RedirectToAction("Index");

           
            return View();
        }
    }
}